const express = require('express');
const PhotosChild = require('../model/PhotosChild')
const router = express.Router()

router.post('/photosChild', async (req, res) => {
    const a = await PhotosChild.findOne({childImg_url: req.body.childImg_url})
    if (a) {
        res.send({
            status: 401,
            msg: '图片已存在！'
        })
    }
    else {
        const newPhotosChild = await new PhotosChild(req.body).save()
        console.log(newPhotosChild);
        res.send({
            status: 200,
            msg: '增加图片成功！'
        })
    }
})

router.get('/getphotosChild', async (req, res) => {
    const photosChildlist = await PhotosChild.find({child_route: req.param("child_route")})
    console.log(photosChildlist);
    res.send({
        status: 200,
        photosChildlist
    })
})

router.delete('/removePhotosChild', async (req, res) => {
    console.log(req.param("_id"));
    const isImgUrl = await PhotosChild.findOne({_id: req.param("_id")})
    if (!isImgUrl) {
        res.send({
            status: 200,
            msg: '删除失败！'
        })
    }
    const removePhotosChildlist = await PhotosChild.remove({_id: req.param("_id")})
    if (removePhotosChildlist){
        res.send({
            status: 200,
            msg: '删除成功！'
        })
    }

})

module.exports = router
